package noahzu.github.io.baselib.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by LvMeng on 15/11/13.
 */
public class UserDefaults {
    private static UserDefaults userDefaults;
    private SharedPreferences sharedPreferences;

    private UserDefaults() {

    }

    /**
     * 初始化
     * @param context Context
     */
    public static void init(Context context) {
        userDefaults = new UserDefaults();
        userDefaults.sharedPreferences = context.getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
    }

    /**
     * 获取单例
     * @return UserDefaults
     */
    public static UserDefaults getInstance() {
        return userDefaults;
    }

    /**
     * 保存数据
     */
    public void setValue(String key, String value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    /**
     * 保存数据
     */
    public void setValue(String key, long value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    /**
     * 保存数据
     */
    public void setValue(String key, boolean value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * 保存数据
     */
    public long getValue(String key, long value) {
        return sharedPreferences.getLong(key, value);
    }

    /**
     * 获取数据
     */
    public boolean getValue(String key, boolean defValue) {
        return sharedPreferences.getBoolean(key, defValue);
    }

    /**
     * 获取数据
     */
    public String getValue(String key, String defValue) {
        return sharedPreferences.getString(key, defValue);
    }


    /**
     * 删除
     */
    public void remove(String key){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(key);
        editor.apply();
    }

    /**
     * 删除所有的KEY
     */
    public void removeAll(){
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.apply();
    }
}
